import { Placement } from '../shared/utils';
import scheduleCallback from '../scheduler/Scheduler';

export function sameNode(a, b) {
  return a && b && a.type === b.type && a.key === b.key;
}

export function deleteRemainingChildren(returnFiber, currentFirstChild) {
  let childToDelete = currentFirstChild;
  while (childToDelete) {
    deleteChild(returnFiber, childToDelete);
    childToDelete = childToDelete.sibling;
  }
}

export function deleteChild(returnFiber, childToDelete) {
  const deletions = returnFiber.deletions;
  if (deletions === null) {
    returnFiber.deletions = [childToDelete];
    return;
  } else {
    returnFiber.deletions.push(childToDelete);
  }
}

export function mapRemainingChildren(currentFirstChild) {
  const existingChildren = new Map();
  let existingChild = currentFirstChild;
  while (existingChild) {
    existingChildren.set(existingChild.key || existingChild.index, existingChild);
    existingChild = existingChild.sibling;
  }
}

export function invokeHooks(wip) {
  const { updateQueue } = wip;

  for (let i = 0; i < updateQueue.length; i++) {
    // 取出每一个副作用对象
    const effect = updateQueue[i];

    // 检查是否有清除方法，有的话就先执行清除方法
    if (effect.destroy) {
      effect.destroy();
    }

    // 接下来就应该执行副作用函数了
    // 注意这里并非直接执行，而是创建一个任务，放入到任务队列中
    scheduleCallback(() => {
      effect.destroy = effect.create();
    });
  }
}
